﻿using System.IO;
using System.Linq;
using System.Reflection;
using Framework.Core.Dependency.Domain;
using Framework.Core.Dependency.Parser;
using Xunit;
using Xunit.Extensions;

namespace Framework.Tests.Core.Dependency
{
    public class Vb6ProjectParserTest
    {
        private readonly Vb6ProjectParser target;

        public Vb6ProjectParserTest()
        {
            target = new Vb6ProjectParser();
        }

        [Theory]
        [InlineData("MUCDealsCapture.vbp", "MUCDealsCapture", "MUCDealsCapture", Language.Vb6, 1, 50, 50)]
        [InlineData("pTitleBar.vbp", "pTitleBar", "MUDTitleBar", Language.Vb6, 1, 2, 1)]
        [InlineData("UsageTrackerVB6TestClient.vbp", "UsageTrackerVB6TestClient", "UsageTrackerVB6TestClient", Language.Vb6, 1, 3, 1)]
        public void Should_parse_vb_proj(string projectName, string expectedProjectName, string expectedAssemblyName, Language expectedLanguage, int expectedProjectCount, int expectedModulesCount, int expectedFilesCount)
        {
            const string sourcePath = @"C:\USERS\developmentBuild";
            var solution = target.Parse(GetProject(projectName), string.Format(@"{0}\CTY.Meteor\Main\Source\Meteor\Com\MUCDealsCapture\{1}", sourcePath, projectName), sourcePath);

            Assert.Equal(expectedProjectName, solution.Name);
            Assert.Equal(expectedLanguage, solution.Language);

            Assert.Equal(expectedProjectCount, solution.Projects.Count());
            var first = solution.Projects.First();

            Assert.Equal(expectedAssemblyName, first.AssemblyName);
            Assert.Equal(expectedModulesCount, first.Modules.Count());
            Assert.Equal(expectedFilesCount, first.Files.Count);
            Assert.Equal("$/CTY.Meteor/Main/Source/Meteor/Com/MUCDealsCapture", solution.VersionControlPath.First());
        }

        private static Stream GetProject(string ressourceName)
        {
            var project = Assembly.GetExecutingAssembly().GetManifestResourceStream("Framework.Tests.Core.Dependency.Vb6Proj." + ressourceName);

            Assert.NotNull(project);

            return project;
        }

        [Fact]
        public void Should_have_shared_files_version_control_source()
        {
            var solution = target.Parse(GetProject("MUCCurveMode.vbp"), @"C:\USERS\developmentBuild\CTY.Meteor\Main\Source\Meteor\Com\MUCCurveMode\MUCCurveMode.vbp", @"C:\USERS\developmentBuild");

            Assert.Equal(2, solution.VersionControlPath.Count());
        }
    }
}
